package com.example.test05; // 注意包名与类所在位置一致

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

// 标记为Spring配置类
@Configuration
// 扫描指定包，自动注册组件
@ComponentScan(basePackages = "com.example.test05")
public class ThreadPoolConfig {

    // 定义线程池Bean，名称为"threadPool"
    @Bean(name = "threadPool")
    public ThreadPoolExecutor createThreadPool() {
        // 线程池参数
        int corePoolSize = 2; // 核心线程数
        int maxPoolSize = 4;  // 最大线程数
        long keepAliveTime = 60; // 空闲时间（秒）
        ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(5); // 任务队列
        ThreadPoolExecutor.AbortPolicy rejectPolicy = new ThreadPoolExecutor.AbortPolicy(); // 拒绝策略

        return new ThreadPoolExecutor(
                corePoolSize,
                maxPoolSize,
                keepAliveTime,
                TimeUnit.SECONDS,
                workQueue,
                rejectPolicy
        );
    }
}
